package com.chenjiajin.controller;

import com.chenjiajin.domain.Logtest;
import com.chenjiajin.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * 被调用方
 */
@RestController
public class DaughterController {

    @Autowired
    private ITestService service;


    @RequestMapping("/aop")
    public String aop(String test) {
        // http://localhost:80/aop
        System.out.println("这是执行方法");
        return "success";
    }


    @RequestMapping("/string")
    public String string(@RequestParam String string) {
        System.err.println("string = " + string);
        return "string";
    }


    @RequestMapping("/doubleString")
    public String doubleString(String string, String name) {
        System.err.println("string = " + string);
        System.err.println("name = " + name);
        return "string";
    }


    @RequestMapping("/feign1")
    public Logtest feign1(@RequestBody Logtest logtest) {
        // http://localhost:80/feign1
        System.err.println(logtest);

        return new Logtest("55555", "66666", "7777", "8888");
    }

    @RequestMapping("/feign2")
    public Logtest feign2(@RequestBody Map<String, Logtest> map) {
        // http://localhost:80/feign2
        System.err.println(map);
        Logtest logtest = map.get("1");
        System.err.println(logtest);
        return new Logtest("55555", "66666", "7777", "8888");
    }

    @RequestMapping("/feign3")
    public Logtest feign3(@RequestBody Map<String, String> map) {
        // http://localhost:80/feign3
        System.err.println(map);
        String logtest = map.get("1");
        System.err.println(logtest);
        return new Logtest("55555", "66666", "7777", "8888");
    }


    @RequestMapping("/feign4")
    public Map<String, String> feign4(@RequestBody Logtest logtest) {
        // http://localhost:80/feign
        System.err.println(logtest);

        Map<String, String> map = new HashMap<>();
        map.put("1", "111");
        map.put("2", "222");
        map.put("3", "333");
        map.put("4", "444");
        return map;

    }

    @RequestMapping("/feign5")
    public Map<String, Logtest> feign5(@RequestBody Logtest logtest) {
        // http://localhost:80/feign
        System.err.println(logtest);

        Map<String, Logtest> map = new HashMap<>();
        map.put("1", new Logtest("1111", "1111", "1111", "1111"));
        map.put("2", new Logtest("2222", "2222", "2222", "2222"));
        map.put("3", new Logtest("3333", "3333", "3333", "3333"));
        map.put("4", new Logtest("4444", "4444", "4444", "4444"));
        return map;

    }

    @RequestMapping("/feign6")
    public List<String> feign6(@RequestBody Logtest logtest) {
        System.err.println(logtest);

        List<String> list = new ArrayList<>();
        list.add("111");
        list.add("222");
        list.add("333");
        return list;

    }

    @RequestMapping("/feign7")
    public List<Logtest> feign7(@RequestBody Logtest logtest) {
        System.err.println(logtest);

        List<Logtest> list = new ArrayList<>();
        list.add(new Logtest("1111", "1111", "1111", "1111"));
        list.add(new Logtest("2222", "2222", "2222", "2222"));
        list.add(new Logtest("3333", "3333", "3333", "3333"));
        return list;

    }

    @RequestMapping("/feign8")
    public List<Logtest> feign8(@RequestBody List<Logtest> list) {
        System.err.println("list.get(0) = " + list.get(0));
        System.err.println(list);

        List<Logtest> list2 = new ArrayList<>();
        list2.add(new Logtest("4444", "4444", "4444", "4444"));
        list2.add(new Logtest("5555", "5555", "5555", "5555"));
        list2.add(new Logtest("6666", "6666", "6666", "6666"));
        return list2;

    }

    //@GetMapping("/testBug")
    //public String testBug(@RequestParam Long i) {
    @GetMapping("/testBug/{i}")
    public String testBug(@PathVariable Long i) {
        System.err.println("i = " + i);
        return "success";
    }


    @GetMapping("/testTime")
    public String testTime() throws InterruptedException {
        int time = 3000;
        Thread.sleep(time);
        System.err.println("time = " + time);
        return "success";
    }

    @RequestMapping("download")
    public void download(@RequestParam String path, HttpServletResponse response) throws IOException {
        System.err.println("path = " + path);
        service.download(response);
    }


    @PostMapping("/feign9")
    public Date feign9(@RequestBody Date date) {
        System.err.println(date.toLocaleString());
        Date date1 = new Date();
        date1.setDate(1);
        return date1;
    }



    @RequestMapping("/feign10")
    public Logtest feign10(@RequestBody Logtest logtest) {
        System.err.println(logtest.getDate().toLocaleString());

        Logtest logtest1 = new Logtest("55555", "66666", "7777", "8888");
        Date date1 = new Date();
        date1.setDate(1);
        logtest1.setDate(date1);
        return logtest1;
    }


}